package com.ehome.poct.core.dao.mybatis;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.mybatisplus.support.AbstractCrudRepository;
import com.ehome.poct.core.api.dto.check.TestReportDataConfigDTO;
import com.ehome.poct.core.dao.entity.check.TestReportDataConfigDO;
import com.ehome.poct.core.dao.mybatis.mapper.TestReportDataConfigMapper;
import com.ehome.poct.core.repository.TestReportDataConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 报告组成配置表RepositoryImpl
 *
 * @author muqiu
 * @Date 2022-03-24 15:48
 */
@Repository
public class TestReportDataConfigRepositoryImpl extends AbstractCrudRepository<TestReportDataConfigDO, TestReportDataConfigDTO> implements TestReportDataConfigRepository {

    @Autowired
    private TestReportDataConfigMapper reportDataConfigMapper;

    @Override
    public List<String> getReportHeaders(String reportCode, Integer stripCode) {
        TestReportDataConfigDO config = new TestReportDataConfigDO().selectOne(Wrappers.<TestReportDataConfigDO>lambdaQuery().eq(TestReportDataConfigDO::getReportCode, reportCode)
                .eq(TestReportDataConfigDO::getStripTypeCode, stripCode));

        return Optional.ofNullable(config).map(c -> StrUtil.splitTrim(c.getDisplayHeaders(), CharUtil.COMMA)).orElse(null);
    }

    @Override
    public List<Integer> getTypeCodes(List<String> unionCode) {
        return reportDataConfigMapper.selectList(Wrappers.<TestReportDataConfigDO>lambdaQuery()
                .in(TestReportDataConfigDO::getReportCode, unionCode).ne(TestReportDataConfigDO::getTestPropertyType,4))
                .stream().map(TestReportDataConfigDO::getStripTypeCode).collect(Collectors.toList());
    }

    @Override
    public Map<String, String> repoctCodeWithTypeCode() {
        List<TestReportDataConfigDO> allConfigs = reportDataConfigMapper.selectList(null);
        Map<String,String> singles=
        allConfigs.stream().filter(t->t.getReportType()==1)
                .map(k->{ return Map.of(k.getReportCode(),k.getStripTypeCode().toString());}).reduce(new HashMap<String, String>(), (m, kv) -> {
            m.putAll(kv);
            return m;
        });
        //手动补充 血常规单测 和121 联测
        singles.put("U_CRP_SAA_BLD_REPORT","121");
        singles.put("R_REPORT","100");
        Map<String, String> collect = allConfigs.stream().filter(it -> it.getReportType() == 2)
                .collect(Collectors.groupingBy(TestReportDataConfigDO::getReportCode,
                        Collectors.mapping(o -> o.getStripTypeCode().toString(), Collectors.joining(","))));
        singles.putAll(collect);

    return singles;
    }

    @Override
    public List<Integer> getAll(List<String> unionCode) {
        return reportDataConfigMapper.selectList(Wrappers.<TestReportDataConfigDO>lambdaQuery()
                .in(TestReportDataConfigDO::getReportCode, unionCode).ne(TestReportDataConfigDO::getTestPropertyType,4))
                .stream().map(TestReportDataConfigDO::getStripTypeCode).collect(Collectors.toList());
    }

    @Override
    public List<TestReportDataConfigDTO> queryByRealStripCodeAndPropertyType(List<Integer> realStripCodeList, Integer testPropertyType) {
        List<TestReportDataConfigDO> configDOList = reportDataConfigMapper.selectList(Wrappers.<TestReportDataConfigDO>lambdaQuery()
                .in(TestReportDataConfigDO::getRealStripTypeCode, realStripCodeList).eq(TestReportDataConfigDO::getTestPropertyType, testPropertyType));
        return BeanUtil.copyToList(configDOList, TestReportDataConfigDTO.class);
    }

    @Override
    public List<TestReportDataConfigDTO> findByStripeCodeIn(List<Integer> stripeCodeList) {
        List<TestReportDataConfigDO> dataConfigDOList = reportDataConfigMapper.findByStripTypeCodeIn(stripeCodeList);
        return BeanUtil.copyToList(dataConfigDOList, TestReportDataConfigDTO.class);
    }
}
